package v1

import (
	"github.com/gin-gonic/gin"
	"tdy-backend/app/core"
	memberService "tdy-backend/app/member/service"
	"tdy-backend/app/panoramic/dto"
	"tdy-backend/app/panoramic/service"
	"tdy-backend/global"
)

type GroupController struct {
}

// @Summary 新增分组
// @Tags 管理端-全景分组
// @Param       token	header string true "登录标识"
// @Param	body	body	dto.GroupAddDto true "新增分组参数"
// @Router /Group/Add [post]
func (o *GroupController) Add(c *gin.Context) {
	var err error
	var param dto.GroupAddDto

	if err = c.ShouldBindJSON(&param); err != nil {
		core.CustomError(c, err.Error())
		return
	}
	db := global.TYD_DB
	token := c.Request.Header.Get("token")
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	groupService := service.GroupService{}
	groupService.MakeORM(db).MakeMemberInfo(memberInfo)
	group, err := groupService.Add(&param)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, group, "分组创建成功")
}

// @Summary 编辑分组
// @Tags 管理端-全景分组
// @Param       token	header string true "登录标识"
// @Param	body	body	dto.GroupEditDto true "编辑分组参数"
// @Router /Group/Edit [post]
func (o *GroupController) Edit(c *gin.Context) {
	var err error
	var param dto.GroupEditDto

	if err = c.ShouldBindJSON(&param); err != nil {
		core.CustomError(c, err.Error())
		return
	}
	db := global.TYD_DB
	token := c.Request.Header.Get("token")
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	groupService := service.GroupService{}
	groupService.MakeORM(db).MakeMemberInfo(memberInfo)
	group, err := groupService.Edit(&param)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, group, "分组编辑成功")
}

// @Summary 删除分组
// @Tags 管理端-全景分组
// @Param       token	header string true "登录标识"
// @Param	body	body	dto.GroupDeleteDto true "删除分组参数"
// @Router /Group/Delete [post]
func (o *GroupController) Delete(c *gin.Context) {
	var err error
	var param dto.GroupDeleteDto

	if err = c.ShouldBindJSON(&param); err != nil {
		core.CustomError(c, err.Error())
		return
	}
	db := global.TYD_DB
	token := c.Request.Header.Get("token")
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	groupService := service.GroupService{}
	groupService.MakeORM(db).MakeMemberInfo(memberInfo)
	err = groupService.Delete(&param)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, nil, "删除成功")
}

// @Summary 分组列表
// @Tags 管理端-全景分组
// @Param       token	header string true "登录标识"
// @Param	WorksId	query	uint true "作品id"
// @Router /Group/ListAll [get]
func (o *GroupController) ListAll(c *gin.Context) {
	var err error
	var param dto.GroupListDto

	if err = c.Bind(&param); err != nil {
		core.CustomError(c, err.Error())
		return
	}
	db := global.TYD_DB
	token := c.Request.Header.Get("token")
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	groupService := service.GroupService{}
	groupService.MakeORM(db).MakeMemberInfo(memberInfo)
	data := groupService.ListAll(&param)
	core.Ok(c, data, "请求成功")
}
